

cd "`c(pwd)'"   // Set working directory to wherever the user placed "replication_package"

* Define global paths relative to the main directory
global main_dir "`c(pwd)'"
global data_dir "$main_dir/data_analysis"
global rawdata_dir "$main_dir/data_raw"
global tables_dir "$main_dir/tables"

cd "$data_dir/"


************************************************************
************************************************************
/*
This do-file creates a dataset with each pair of firms that do not directly share  a director, but that share a director with a third firm.

1) creates overlapping_directors_pop 
2) creates POP OD density
*/

************************************************************
* 1) POP overlapping dirtectors
use overlapping_directors, clear
keep year gvkey1 gvkey2 directorid1 directorid2 directorid3 directorid4 directorid5 directorid6 directorid7 directorid8 directorid9 directorid10 directorid11 directorid12 directorid13
save temp_base, replace
use temp_base, clear
rename gvkey1 gvkey3
keep year gvkey3 gvkey2
sort year gvkey2 gvkey3
save temp, replace
rename gvkey2 gvkey1
sort year gvkey1 gvkey3
save temp2, replace
use temp_base, clear
sort year gvkey2 
joinby year gvkey2 using temp, unmatched(master)
drop _merge
drop if gvkey1==gvkey3
sort year gvkey1 gvkey3
merge year gvkey1 gvkey3 using temp2
keep if _merge==1
drop _merge
drop gvkey2
rename gvkey3 gvkey2
sort year gvkey1 gvkey2
egen pair=group(gvkey1 gvkey2)
sort pair year
bys pair year: egen size_popodlink=count(pair)
sort pair year
drop if pair==pair[_n-1] & year==year[_n-1]
drop pair  
save overlapping_directors_pop, replace
use overlapping_directors_pop, clear
rename gvkey1 temp
rename gvkey2 gvkey1
rename temp gvkey2
save overlapping_directors_pop_transf, replace
append using overlapping_directors_pop
save overlapping_directors_pop_double, replace
*****************************************************************
* 2) POP TNIC4 OD density
use overlapping_directors_pop_double, clear
drop if gvkey1==gvkey2
g overdirs=1
g overdirs2=1
g overdirs4=1
g overdirs2_4=1
capture drop _merge
joinby gvkey1 gvkey2 year using tnic2_data, unmatched(master)
drop if _merge!=3
replace overdirs2=0 if _merge!=3
drop _merge score
joinby gvkey1 gvkey2 year using tnic4_data, unmatched(master)
replace overdirs4=0 if _merge!=3
replace overdirs2_4=0 if _merge==3
drop _merge score
rename gvkey1 gvkey
drop gvkey2
collapse (sum) overdirs overdirs2  overdirs4    overdirs2_4 , by(year gvkey)
save temp_2, replace
use tnic2_data, clear
rename gvkey1 gvkey
joinby gvkey year using sample_od, unmatched(master)
drop if _merge!=3
drop _merge
rename gvkey gvkey1
rename gvkey2 gvkey
joinby gvkey year using sample_od, unmatched(master)
drop if _merge!=3
drop _merge
rename gvkey gvkey2
g tnpeers2=1
g tnpeers4=1
g tnpeers2_4=1
capture drop _merge
joinby gvkey1 gvkey2 year using tnic4_data, unmatched(master)
replace tnpeers4=0 if _merge!=3
replace tnpeers2_4=0 if _merge==3
drop _merge score
rename gvkey1 gvkey
drop gvkey2
collapse (sum) tnpeers2  tnpeers4  tnpeers2_4 , by(year gvkey)
save temp2_2, replace
use temp2_2, clear
merge year gvkey using temp_2
drop if _merge ==2
drop _merge
replace overdirs=0 if overdirs==.
replace overdirs2=0 if overdirs2==.
replace overdirs4=0 if overdirs4==.
replace overdirs2_4=0 if overdirs2_4==.
g odrat2=overdirs2/tnpeers2
g odrat4=overdirs4/tnpeers4
g odrat2_4=overdirs2_4/tnpeers2_4
sort year gvkey
save jh_overdirs2popod, replace
sum
use tnic2_data, clear
drop if gvkey1==gvkey2
rename gvkey2 gvkey
sort year gvkey
joinby year gvkey using jh_overdirs2popod, unmatched(master)
keep if _merge==3
drop _merge score
rename gvkey gvkey2
keep gvkey1 gvkey2 year odrat2  odrat4   odrat2_4 
sort gvkey1 gvkey2 year
joinby gvkey1 gvkey2 year using tnic4_data, unmatched(master)
replace odrat4=. if _merge!=3
replace odrat2_4=. if _merge==3
drop _merge score
rename gvkey1 gvkey
sum
collapse (mean) odrat2 odrat4 odrat2_4 , by(year gvkey)
sort year gvkey
rename odrat2 popoddensity2
rename odrat4 popoddensity4
rename odrat2_4 popoddensity2_4
save popod_density, replace




